<?php
/*************************************************

个人中心主模型 - the 
Author: WangHuabin <17-10-23>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class PersonController extends CommonController {
   //2018-3-19新改版账户中心
    public function index() {
        $user_id = $_SESSION['user']['id'];
        if($user_id) {
            $map['id'] = $user_id;
            $user_info = D('User')->get_one($map);
            if($user_info['role']==2) {
                header("location:/Home/Person/push_index");
            } else {
                $this->assign('user_info',$user_info);
            }
        }else{
            header("location:/Home/User/login");
        }
        $this->display();
    }
	  //2018-3-19新改版投顾账户中心
    public function push_index() {
        if(!$_SESSION['user']['id']){
            header('Location:/Home/User/login');die;
        }
        $map['id'] = $_SESSION['user']['id'];
        $user_info = D('User')->get_one($map);
        if($user_info['role']==2) {
            $map = array();
            $map['user_id'] = $_SESSION['user']['id'];
            $recommend = D('Recommend')->get_one($map);
            if($recommend){
                $map = array();
                $map['referral'] = $_SESSION['user']['id'];
                $count_user = D('User')->count($map);
                $this->assign('count_user',$count_user);
                $this->assign('recommend',$recommend);
            }else{
                header('Location:/Home/Recommend/apply_pusher');die;
            }
        } else {
            header("location:/Home/Person/index");
        }
        $this->assign('user_info',$user_info);
        $this->display();
    }

    //2018-3-20新改版个人中心
    public function person_index() {
        //用户基础信息
        $user_id = I('get.user_id');
        $map['id'] = $user_id;
        $user = D('User')->get_one($map);
        //用户投顾信息
        $map = array();
        $map['user_id'] = $user_id;
        $recom = D('Recommend')->get_one($map);
        //统计深度评测
        $depth_count = D('Depth')->count($map);
        //统计信披解读
        $announce_count = D('CircleAnnounce')->count($map);
        //统计舆情解读
        $opinion_count = D('CircleOpinion')->count($map);
        //统计平台短评
        $commentary_count = D('PlatformCommentary')->count($map);
        //红包
        $packet = R('Packet/get_redpacket',array('userid'=>$user_id,'platform_id'=>''));
        foreach($packet as $k => $v) {
            foreach($v['scheme'] as $kk => $vv) {
                $packet[$k]['scheme'][$kk]['start_amount'] = floatval($vv['start_amount']);
            }
        }
        $this->assign('packet',$packet);
        $this->assign('commentary_count',$commentary_count);
        $this->assign('opinion_count',$opinion_count);
        $this->assign('announce_count',$announce_count);
        $this->assign('depth_count',$depth_count);
        $this->assign('recom',$recom);
        $this->assign('user_info',$user);
        $this->display();
    }
	
	
	  //2018-3-20新改版聊天列表
    public function message_index() {
		
        $user_id = $_SESSION['user']['id'];
        if($user_id){
			
            $chat = $this->get_friends(3);
			//print_r($chat);die;
            $this->assign('chat',$chat);
			
            $map['id'] = $user_id;
            $user = D('User')->get_one($map,$field="id,nickname");
			
            //公告
            $map = array();
            $cate_id = 39;
            $map['cate_id'] = $cate_id;
            $new_count = D('NewsHouse')->count($map);
            $map = array();
            $map['user_id'] = $user_id;
            $notice_new = D('NewsNotice')->where($map)->count();
            $notice_read = $new_count-$notice_new;
            if($notice_read>0) {
                //存在未读
                $notice_reads = 1;
            } else {
                $notice_reads = 0;
            }
            //站内信
            $mes = D('Message')->get_all($map);
            foreach($mes as $k => $v) {
                if($v['is_read']==0) {
                    $unread[] = $v['is_read'];
                }
            }
			
            $this->assign('unread',$unread);
            $this->assign('notice_reads',$notice_reads);
            $this->assign('user',$user);
        }else{
			
            header("location:/Home/User/login");
        }
        $this->display();
    }
	
	  /*互动通知*/
    public function interact_message(){
        if($_SESSION['user']['id']){
         //查询动态
            $r = $this->get_trend($_SESSION['user']['id'],1);
            if($r){
                 //时间排序
                $sort = array(  
                'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序  
                'field'     => 'add_time',       //排序字段  
                ); 

                $arrSort = array();  
                foreach($r AS $uniqid => $row){  
                    foreach($row AS $key=>$value){  
                        $arrSort[$key][$uniqid] = $value;  
                    }  
                }  
                if($sort['direction']){  
                    array_multisort($arrSort[$sort['field']],constant($sort['direction']),$r);  
                }
                $r=array_slice($r,0,5);
            }
            $this->assign('r',$r);
            $this->display();
        }else{
             header("location:/");
        }
       
    }
	    //2018-3-21新改版加入圈子详情
    public function my_circle() {
        //头部加入所有圈子列表
        $map['status'] = 0;
        $map['id'] = array('not in','2,5,7');
        $plat = D('Platform')->get_all($map,$field="id,name,wx_square_logo");
        $plats = $plat[0];
        //单个圈子动态详情
        $map = array();
        $id = I('get.id');
        if(!$id) {
            $id = $plats['id'];
        }
        $map['id'] = $id;
        $plat_name = D('Platform')->get_one($map,$field="id,name");
        //$page=0;
        //$pagenum=5;
        $map = array();
        $map['status'] = 1;
        $map['platform_id'] = $id;
        $pcomm = D('PlatformCommentary')->get_all_page($map);
        $userid = $_SESSION['user']['id'];
        $map['status'] = 1;
        $opinion = D('CircleOpinion')->get_all($map);
        foreach ($opinion as $k => $v) {
            $v['type']=4;
            $pcomm[]= $v;
        }
        $map = array();
        foreach ($pcomm as $k => $v) {
            if($v['type']!=4){
                $v['tag'] = explode(',',$v['tag']);
                $v['type']=1;
            }

            $map['id'] = $v['user_id'];
            $v['user'] =D('User')->get_one($map);
            $map = array();
            $map['user_id'] = $v['user_id'];
            $map['platform_id'] = $v['platform_id'];
            $v['grade'] = D('PlatformGrade')->get_one($map);

            $add_time = $v['add_time'];
            //判断是否点赞
            if($userid){
                $where['id_user'] = $userid;
                if($v['type']==4){
                     $where['type'] =8;
                }else{
                     $where['type'] = 6;
                }

                $where['id_target'] = $v['id'];
                $praise = D('Praise')->get_one($where);
                if($praise){
                    $v['is_praise'] = 1;
                }else{
                    $v['is_praise'] = 0;
                }
            }else{
                    $v['is_praise'] = 0;
            }
            $long = time()-$add_time;

            if($long<60){
                $v['add_date'] = $long."秒前";
            }else if($long<3600){
                $v['add_date'] = ceil($long/60)."分钟前";
            }else if($long>3600 && $long<86400){
                $v['add_date'] = ceil($long/3600)."小时前";
            }else if($long>86400 && $long<259200){
                $v['add_date'] = ceil($long/86400)."天前";
            }else if($long>259200){
                $v['add_date'] = date('Y-m-d H:i:s',$add_time);
            }else if($long=0){
                 $v['add_date'] = "刚刚";
            }
            $push_evaluate[]= $v;

        }
        //print_r($pcomm);die;
        //合并推手的深度评测
        $map = array();
        $map['platform_id'] = $id;
        $map['status'] = 1;
        $depth = D('Depth')->get_all($map);
        foreach ($depth as $k => $v) {
            $v['type']=2;
             if($userid){
                $where['id_user'] = $userid;
                $where['type'] = 7;
                $where['id_target'] = $v['id'];
                $praise = D('Praise')->get_one($where);
                if($praise){
                    $v['is_praise'] = 1;
                }else{
                    $v['is_praise'] = 0;
                }
            }else{
                    $v['is_praise'] = 0;
            }
            $map = array();
            $map['user_id'] = $v['user_id'];
            $map['platform_id'] = $v['platform_id'];
            $v['grade'] = D('PlatformGrade')->get_one($map);
            $push_evaluate[]= $v;

        }
        //合并信批
        $map = array();
        $map['platform_id'] = $id;
        $map['status'] = 1;
        $announce = D('CircleAnnounce')->get_all($map);
        foreach ($announce as $k => $v) {
            $v['type']=3;
            if($userid){
                $where['id_user'] = $userid;
                $where['type'] = 9;
                $where['id_target'] = $v['id'];
                $praise = D('Praise')->get_one($where);
                if($praise){
                    $v['is_praise'] = 1;
                }else{
                    $v['is_praise'] = 0;
                }
            }else{
                    $v['is_praise'] = 0;
            }
            $map = array();
            $map['user_id'] = $v['user_id'];
            $map['platform_id'] = $v['platform_id'];
            $v['grade'] = D('PlatformGrade')->get_one($map);
            $push_evaluate[]= $v;

        }
        foreach ($push_evaluate as $k => $v) {
            $map = array();
            $map['id'] = $v['user_id'];
            $push_evaluate[$k]['user'] =D('User')->get_one($map);
            $add_time = $v['add_time'];
            $long = time()-$add_time;
            if($long<60){
                $push_evaluate[$k]['add_date'] = $long."秒前";
            }else if($long<3600){
                $push_evaluate[$k]['add_date'] = ceil($long/60)."分钟前";
            }else if($long>3600 && $long<86400){
                $push_evaluate[$k]['add_date'] = ceil($long/3600)."小时前";
            }else if($long>86400 && $long<259200){
                $push_evaluate[$k]['add_date'] = ceil($long/86400)."天前";
            }else if($long>259200){
                $push_evaluate[$k]['add_date'] = date('Y-m-d H:i:s',$add_time);
            }else if($long=0){
                $push_evaluate[$k]['add_date'] = "刚刚";
            }
        }
        //print_r($push_evaluate);die;
        $sort = array(
        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
        'field'     => 'add_time',       //排序字段
        );

        $arrSort = array();
        foreach($push_evaluate AS $uniqid => $row){
            foreach($row AS $key=>$value){
                $arrSort[$key][$uniqid] = $value;
            }
        }
        if($sort['direction']){
            array_multisort($arrSort[$sort['field']],constant($sort['direction']),$push_evaluate);
        }
        $push_evaluate=array_slice($push_evaluate,$page,$pagenum);
        //print_r($push_evaluate);
        $this->assign('id',$id);//平台id
        $this->assign('plat_name',$plat_name);
        $this->assign('push_evaluate',$push_evaluate);
        $this->assign('plat',$plat);
        $this->display();
    }

    /******************
    ****2018-3-21新增举报功能模块******
    ******************/
    public function report() {
        $user_id = $_SESSION['user']['id'];
        if($user_id) {
            $option = I('post.');
            $option['option'] = implode(',',$option['option']);
            $option['user_id'] = $user_id;
            $option['add_time'] = time();
            $report = M('Report')->data($option)->add();
            if($report) {
                $data = array('err'=>0,'msg'=>"举报成功");
            } else {
                $data = array('err'=>1,'msg'=>"举报失败");
            }
        } else {
            $data = array('err'=>2);
        }
        $this->ajaxReturn($data);

    }
	
	    //举报功能检测是否登录
    public function check_report() {
        $user_id = $_SESSION['user']['id'];
        if($user_id) {
            $data = array('err'=>0);
        } else {
            $data = array('err'=>1);
        }
        $this->ajaxReturn($data);
    }
	  //推手賬戶中心首頁
    public function invest_push_index(){
        //判断推手

        $map['user_id'] = $_SESSION['user']['id'];
        if(!$_SESSION['user']['id']){
            header('Location:/Home/person/index');die;
        }

        $recommend = D('Recommend')->get_one($map);
        if($recommend){
            if($recommend['role']==1){
                $map = array();
                $map['referral'] = $_SESSION['user']['id'];
                $tj_count_id = D('User')->get_all($map);
                foreach($tj_count_id as $k=>$v){
                    $user_id_push[] = $v['id'];
                }
                if($user_id_push){
                    $user_id_push_count = count($user_id_push);
                    
                     $map = array();
                     $map['user_id'] = array('in',$user_id_push);
                     $u_obj = D('UserObject')->group_count($map); //投资用户
                     $tj_count = count($u_obj);
                        if($tj_count>=5){
                             $map = array();
                             $map['id'] = $recommend['id'];
                             $data['role']=2;//转正
                             $r = D('Recommend')->edit($map,$data);
                             R('Msg/sendmail',array($typename='push_msg',$userid = $_SESSION['user']['id']));
                             $push = 1;
                             $tag = "正式推手";
                        }
                }else{
                    $tj_count=0;
                    $push = 0;
                    $tag = "见习推手";

                }
                
            }elseif ($recommend['role']==2){
                $push = 1;
                $tag = "正式推手";
            }
        }else{
            header('Location:/Home/Recommend/apply_pusher');die;
        }
        $recommend_time = date('Y-m-d 0:0:0',$recommend['add_time']);
        $recommend_time = strtotime($recommend_time);
        $push_long = ceil((time()-$recommend_time)/3600/24);
        //cpa cps 总收益
        //CAP收益
       
        $map['user_id'] = $_SESSION['user']['id'];
        $map['platform_id'] = array('neq',11);
        $map['type'] = 3;
        $cpa_count = D('Bill')->sum($map);
		//echo  D('Bill')->_sql();
        //cps
        $map['type'] = 4;
        $cps_count = D('Bill')->sum($map);
       // echo  D('Bill')->_sql();
          //ROI返利总额
        $map['platform_id'] = array('eq',11);
        $map['type'] = array(array('eq',3),array('eq',4), 'or');
        $roi_count = D('Bill')->sum($map);
        
        
        //cpa cps 当日数据
        $starttime = strtotime(date('Y-m-d 0:0:0',time()-3600*24));
        $map['add_time'] = array('between',$starttime.','.time());
        $map['platform_id'] = array('neq',11);
        $map['type'] = 3;
        $cpa_today = D('Bill')->sum($map);
        $map['type'] = 4;
        $cps_today = D('Bill')->sum($map);

        $map['add_time'] = array('between',$starttime.','.time());
        $map['platform_id'] = array('eq',11);
        $map['type'] = array(array('eq',3),array('eq',4), 'or');
        $roi_today = D('Bill')->sum($map);

        
        
        $cpa_today = $cpa_today?$cpa_today:'0.00';
        $cps_today = $cps_today?$cps_today:'0.00';
        $roi_today = $roi_today?$roi_today:'0.00';

        $cps_count = $cps_count?$cps_count:'0.00';
        $cpa_count = $cpa_count?$cpa_count:'0.00';
        $roi_count = $roi_count?$roi_count:'0.00';

        //累计返现
		//echo $cpa_count;
        $count_fx=$cps_count+$cpa_count+$roi_count;
            //昨天返利额
        $today_fx_count = $cps_today+$cpa_today+$roi_today;


        //注册用户
        $map = array();
        $map['referral'] = $_SESSION['user']['id'];
        $map['add_time'] = array('between',$starttime.','.time());
        $push_user_count = M('UserPlatform')->where($map)->count();
        //推手推广的所有用户ID集合
        $map = array();
        $map['referral'] = $_SESSION['user']['id'];
        $tj_user = D('User')->get_all($map);
        //推荐用户的ID集合
        foreach ($tj_user as $k => $v) {
            $tj_user_id[] = $v['id'];
        }
        if($tj_user_id){
            $map['user_id'] = array('in',$tj_user_id);
            $map['type'] = array('in','1,2'); //投资而不是推手返利
            $map['add_time'] = array('between',$starttime.','.time());//今日
            $today_tz_user_count = D('UserObject')->group_count($map);
            $today_tz_user_count = count($today_tz_user_count);
        }else{
            $today_tz_user_count = 0;
        }
       
        // echo $push_user_count;
        //推广总收益
        $count_push_money = $cps_count+$cpa_count+$roi_count;
        //可提现余额
        $map = array();
        $map['id'] = $_SESSION['user']['id'];
        $user_info = D('User')->get_one($map);
        $user_balance = $user_info['prize_balance'];
        //合作平台明细
        $hz_plat_info = $this->hz_platinfo($_SESSION['user']['id']);
        if($hz_plat_info){
            $hz_plat_info_count = count($hz_plat_info);
            foreach ($hz_plat_info as $k => $v) {
            $map = array();
            $plat_id = $v['platform_id'];
            $map['platform_id'] = $plat_id;
            $platform_scheme = D('PlatformScheme')->where($map)->select();
                foreach($platform_scheme as $ke=>$va){
                    if($va['user_roi']>0){
                        $platform_scheme[$ke]['user_roi_count'] =$va['start_amount']*$va['user_roi']/100;
                        $platform_scheme[$ke]['push_roi_count'] =$va['start_amount']*$va['push_roi']/100;
                    }
                }
            $hz_plat_info[$k]['platform_scheme'] = $platform_scheme;
            // print_r($platform_scheme);
            }

        }else{
            $hz_plat_info_count = 0;
        }

        //print_r($hz_plat_info);
        $map =array();
        $map['status'] = 0;
        $plat_count = D('Platform')->count($map);
        $un_hz_count = $plat_count-$hz_plat_info_count;


        //我的投资人总览
        $map['referral'] = $_SESSION['user']['id'];
        $user_count =  D('User')->count($map); //总推广用户数
        $user_push_list =  D('User')->get_all($map); //总推广用户数
        foreach($user_push_list as $k=>$v){
            $user_id_push[] = $v['id'];
        }
        if($user_id_push){
             $user_id_push_count = count($user_id_push);
            // print_r($user_id_push);
             $map = array();
             $map['user_id'] = array('in',$user_id_push);
             $u_obj = D('UserObject')->group_count($map); //投资用户
             $u_obj_count = count($u_obj); //已投资用户总数
             $un_obj_count = $user_id_push_count-$u_obj_count;//未投资用户总数
            $map = array();
            $map['first'] = 1;
            $map['user_id'] = array('in',$user_id_push);
            $first_tj_count = D('UserObject')->group_count($map);  //首投
            $first_tj_count = count($first_tj_count);
            $map['first'] = 0;

            $ft_tj_count = D('UserObject')->group_count($map);  //复投
            $ft_tj_count = count($ft_tj_count);

            $map['user_id'] = array('in',$user_id_push);
            $map['type'] = 1;
            $cpa_user_count = D('Bill')->sum($map);  //用户首投返利总额
            //cps
            $map['type'] = 2;
            $cps_user_count = D('Bill')->sum($map); //用户加息返利总额
        }else{
            $u_obj='';
            $u_obj_count=0;
            $un_obj_count=0;
            $first_tj_count=0;
            $ft_tj_count=0;
            $cpa_user_count=0;
            $cps_user_count=0;
        }
       
        //echo $cps_user_count;
        // echo $first_tj_count;
        // 
        $map = array();
        $map['id'] = $_SESSION['user']['id'];
        $user_info = D('User')->get_one($map);
        $un_plat_list = $this->un_hz_plat($_SESSION['user']['id']);
        
       // 
        $this->assign('push',$push);
        $this->assign('count_fx',$count_fx);
        $this->assign('today_fx_count',$today_fx_count);
        $this->assign('roi_count',$roi_count);
        $this->assign('user_info',$user_info);
        $this->assign('push_long',$push_long);
        $this->assign('un_plat_list',$un_plat_list);
        $this->assign('user_id_push_count',$user_id_push_count);
        $this->assign('u_obj_count',$u_obj_count);
        $this->assign('un_obj_count',$un_obj_count);
        $this->assign('first_tj_count',$first_tj_count);
        $this->assign('ft_tj_count',$ft_tj_count);
        $this->assign('cpa_user_count',$cpa_user_count);
        $this->assign('cps_user_count',$cps_user_count);
        $this->assign('hz_plat_info_count',$hz_plat_info_count);
        $this->assign('un_hz_count',$un_hz_count);
        $this->assign('tag',$tag);
        $this->assign('hz_plat_info',$hz_plat_info);
        $this->assign('roi_count',$roi_count);
        $this->assign('cpa_count',$cpa_count);
        $this->assign('cps_count',$cps_count);
        $this->assign('roi_today',$roi_today);
        $this->assign('cpa_today',$cpa_today);
        $this->assign('cps_today',$cps_today);
        $this->assign('count_push_money',$count_push_money);
        $this->assign('user_balance',$user_balance);
        $this->assign('push_user_count',$push_user_count);
        $this->assign('today_tz_user_count',$today_tz_user_count);
        $this->display();
    }

	
	  //投資者賬戶中心首頁
    public function invest_index(){
        //unset($_SESSION);
         $user_id = $_SESSION['user']['id'];
         if($user_id){
                $map['id'] = $user_id;
                $user_info = D('User')->get_one($map);
                //历史数据
                //平台投资总额
                $starttime=strtotime(date('Ymd',time()-86400));
                $endtime=time();
                $map = array();
                $map['user_id'] = $user_id;
                $plat_count = D('UserObject')->obj_sum($map);
                $map['add_time'] = array('between',$starttime.','.$endtime);
                $today_plat_count = D('UserObject')->obj_sum($map);
                //首投返利总额
                $map = array();
                $map['user_id'] = $user_id;
                $map['type'] = 1;
				$map['platform_id'] = array('neq',11);
                $st_count = D('Bill')->m_sum($map);
                $map['add_time'] = array('between',$starttime.','.$endtime);
                $today_st_count = D('Bill')->m_sum($map);

                //加息
                $map = array();
                $map['user_id'] = $user_id;
                $map['platform_id'] = array('neq',11);
                $map['type'] = 2;
                $ft_count = D('Bill')->m_sum($map);
                $map['add_time'] = array('between',$starttime.','.$endtime);
                $today_ft_count = D('Bill')->m_sum($map);

                //ROI返利总额
                $map = array();
                $map['user_id'] = $user_id;
                $map['platform_id'] = array('eq',11);
                $map['type'] = array(array('eq',1),array('eq',2), 'or');
                $roi_count = D('Bill')->m_sum($map);
                $map['add_time'] = array('between',$starttime.','.$endtime);
                $today_roi_count = D('Bill')->m_sum($map);

                //累计返现
                $count_fx=$st_count+$ft_count+$roi_count;
				//echo $roi_count;
                //昨天返利额
                $today_fx_count = $today_st_count+$today_ft_count+$today_roi_count;



                //平台数据
                $map = array();
                $map['user_id'] = $user_id;
                $plat_bing_count = M('UserPlatform')->where($map)->count();
                $tz_count = M('UserObject')->where($map)->field('count(*)')->group('platform_id')->select();
                $tz_count= count($tz_count);

                //绑定平台列表
                $page = 1;
                $pagenum = 100;
				$map = array();
                //$list_platform_list = D('UserPlatform')->where($map)->limit(($page-1)*$pagenum,$pagenum)->select();
                //$list_platform_list = D('Platform')->where($map)->limit(($page-1)*$pagenum,$pagenum)->select();
				$list_platform_list = D('Platform')->get_all_page($map='',$page-1,$pagenum,$order='`order` asc');
                $page_count = D('UserPlatform')->where($map)->count();
                $page_num = ceil($page_count/10);
                // $page_count = D('UserPlatform')->where($map)->count();
                // echo $page_count;
				
                foreach ($list_platform_list as $k => $v) {
                    $platform_id = $v['id'];
                    $map = array();
                    //$map['id'] = $platform_id;
                    $map['_string'] ='id = '.$platform_id.' and id <>2 ';
                    $platform_list[$k] = D('Platform')->get_one($map);
                    $platform_list[$k]['bing_time'] = $v['add_time'];
                }
				//print_r($platform_list)
                if($platform_list){
                    foreach ($platform_list as $k => $v) {
                        //投资标的数
                        $map = array();
                        $map['platform_id'] = $v['id'];
                        $map['user_id'] = $user_id;
                        $obj_count= D('UserObject')->count($map);
                        if(!$obj_count){
                             $platform_list[$k]['obj_count'] = '0';
                        }else{
                             $platform_list[$k]['obj_count'] = $obj_count;
                        }
                        //投资总额
                        $map = array();
                        $map['user_id'] = $user_id;
                        $map['platform_id'] = $v['id'];
                        $plat_count = D('UserObject')->obj_sum($map);
                        if(!$obj_count){
                             $platform_list[$k]['plat_count'] = '0.00';
                        }else{
                             $platform_list[$k]['plat_count'] = $plat_count;
                        }

                        

                        //返利总额
                        $map = array();
                        $map['user_id'] = $user_id;
                        $map['type'] = array('in','1,2');
                        $map['platform_id'] = $v['id'];
                        $fl_count = D('Bill')->m_sum($map);
                        if(!$obj_count){
                             $platform_list[$k]['fl_count'] = '0.00';
                        }else{
                             $platform_list[$k]['fl_count'] = $fl_count;
                        }
                        if($v['status']==1 or !$v['id']){
                            unset($platform_list[$k]);
                        }
                    }
                }
                //检查是否为推手
                $map = array();
                $map['user_id'] = $user_id;
                $recommend = D('Recommend')->get_one($map);
				if(!$recommend){
                    $map = array();
                    $map['user_id'] = $user_id;
                    $map['status'] = 0;
                    $is_apply = D('RecommendApply')->get_one($map);
                    if($is_apply){
                        $is_apply=1;
                        $this->assign('is_apply',$is_apply);
                    }
                }

            //    print_r($recommend);
                $this->assign('recommend',$recommend);
                //print_r($platform_list);
                $this->assign('count_fx',$count_fx);
                $this->assign('today_fx_count',$today_fx_count);


                $this->assign('page_num',$page_num);
                $this->assign('platform_list',$platform_list);
                $this->assign('user_info',$user_info);
                $this->assign('plat_count',$plat_count);
                $this->assign('st_count',$st_count);
                $this->assign('ft_count',$ft_count);
                $this->assign('roi_count',$roi_count);
                $this->assign('today_roi_count',$today_roi_count);
                $this->assign('today_plat_count',$today_plat_count);
                $this->assign('today_st_count',$today_st_count);
                $this->assign('today_ft_count',$today_ft_count);
                $this->assign('plat_bing_count',$plat_bing_count);
                $this->assign('tz_count',$tz_count);


         }else{
             header("location:/Home/User/login");
         }
        $this->display();
    }
    

     //合作平台投资明细
    public function hz_platinfo($user_id,$platform_name='',$starttime='',$endtime=''){
        // $user_id = 444;
        //合作平台
        $map['user_id'] = $user_id;
        $recommend = D('Recommend')->get_one($map);
        $map['recommend_id'] = $recommend['id'];
        $map['status'] = 1;
        if($starttime && $endtime){
            $map['add_time'] = array('between',$starttime.','.$endtime);
        }
		$map['platform_id'] = array('not in','20,1,17');
        $r_plat = D('RecommendPlatform')->get_all($map);
        // print_r($r_plat);
        // echo D('RecommendPlatform')->_sql();
        if($r_plat){
            //合作平台id集合
            foreach ($r_plat as $k => $v) {
                $platform_id[] = $v['platform_id'];
            }
             //平台注册用户
            $map = array();
            $map['referral'] = $user_id;
            $u_list =M('User')->where($map)->select();
            
            if($u_list){
                foreach ($u_list as $key => $value) {
                $u_plat_user_id[]=$value['id'];
                }
            }
            //print_r($u_plat_user_id);

            $map['platform_id'] = array('in',$platform_id);
            $u_plat =M('UserPlatform')->where($map)->select();
          /*  print_r($u_plat);*/
            if($u_plat){
                foreach ($u_plat as $key => $value) {
                $u_plat_user_id[]=$value['user_id'];
                }
            }
            //合作平台投资明细
            foreach($r_plat as $k=>$v){
                $platform_id = $v['platform_id'];
                $map = array();
                $map['id'] = $platform_id;

                if($platform_name){
                    $map['name'] = array('like', '%'.$platform_name.'%'); ;
                }

                $platform_info= D('Platform')->get_one($map);
                if($platform_info){
                    $r_plat[$k]['platform_info'] = $platform_info;
                    if($platform_info['coo_end_date']>=time()){
                        $r_plat[$k]['coo_status'] = "推广期";
                    }else{
                        $r_plat[$k]['coo_status'] = "返利期";
                    }
                    
                }else{
                    unset($r_plat[$k]);
                }
               
                if($u_plat){
                    //平台注册总数
                    $where['user_id'] = array('in',$u_plat_user_id);
                    $where['platform_id'] = $v['platform_id'];
                    $u_plat_count =M('UserPlatform')->where($where)->count();
                    
                    //查询平台投资人数
                    $map = array();
                    $map['user_id'] = array('in',$u_plat_user_id);
                    $map['platform_id'] = $v['platform_id'];
                    $group = 'user_id';
                    $tj_list = D('UserObject')->group_count($map,$group);  
                    //echo D('UserObject')->_sql();
                    $r_plat[$k]['tj_list_count'] = count($tj_list); //平台投资人数
                    if($u_plat_count){
                        $un_tz_count = $u_plat_count-count($tj_list); //已注册为投资人数
                        if($un_tz_count>=0){
                            $r_plat[$k]['un_tz_count'] = $un_tz_count; //已注册为投资人数
                        }else{
                            $r_plat[$k]['un_tz_count'] =0;
                        }
                    }
                    //首投返利金额
                    $map = array();
                    $map['user_id'] = $user_id;
                    $map['type'] = 3;
                    $map['platform_id'] = $v['platform_id'];
                    $plat_st_count = D('Bill')->m_sum($map);
                    $r_plat[$k]['plat_st_count'] = $plat_st_count?$plat_st_count:'0.00';
                    //复投返利金额
                    $map['type'] = 4;
                    $plat_ft_count= D('Bill')->m_sum($map);
                    $r_plat[$k]['plat_ft_count'] = $plat_ft_count?$plat_ft_count:'0.00';
                    //总投资额
                    $map = array();
                    $map['platform_id'] = $v['platform_id'];
                    $map['user_id'] = array('in',$u_plat_user_id);
                    $tz_count = D('UserObject')->obj_sum($map);  
                    $r_plat[$k]['count_tz'] =$tz_count;
                    //平台总收益
                    $map = array();
                    $map['user_id'] = $user_id;
                    $map['platform_id'] = $v['platform_id'];
                    $map['type'] =array('in','3,4');
                    $r_plat[$k]['plat_profit'] = D('Bill')->m_sum($map);
                    $r_plat[$k]['plat_st_count'] = $plat_st_count;
                }else{
                    if($platform_info){
                        $r_plat[$k]['tj_list_count'] =0;
                        $r_plat[$k]['un_tz_count'] =0;
                        $r_plat[$k]['plat_st_count'] = 0;
                        $r_plat[$k]['plat_ft_count'] = 0;
                        $r_plat[$k]['count_tz'] = 0;
                        $r_plat[$k]['plat_profit']=0;
                    }
                    
                }
                
                
                
            }

            

        }else{
            $r_plat = '';
        }

        return $r_plat;
    }

    //未合作平台
    public function un_hz_plat($user_id){
        $map['user_id'] = $user_id;
        $recommend = D('Recommend')->get_one($map);
        $map['recommend_id'] = $recommend['id'];
        $map['status'] = 1;
        $r_plat = D('RecommendPlatform')->get_all($map);
        if($r_plat){
            //合作平台id集合
            foreach ($r_plat as $k => $v) {
                $platform_id[] = $v['platform_id'];
            }
            $map['status'] = 0;
            $map['id'] =array('not in',$platform_id);
            $platform_list = D('Platform')->get_all($map);
        }else{
            $map['status'] = 0;
            $platform_list = D('Platform')->get_all($map);
        }
        foreach($platform_list as $k=>$v){
            $platform_id = $v['id'];
            $map = array();
            $map['platform_id'] = $platform_id;
            $map['recommend_id'] =  $recommend['id'];
            $map['status'] = 0;//申请中
            $r = D('RecommendPlatform')->get_one($map);
            if($r){
                $platform_list[$k]['is_apply'] = 1;
            }else{
                $platform_list[$k]['is_apply'] = 0;
            }
        }
        return $platform_list;
    }


    //公告站内信
    public function notice() {
        $user_id = $_SESSION['user']['id'];
        //$user_id = 8;
        if($user_id) {
            $map['id'] = $user_id;
            $user = D('User')->get_one($map);
            $map = array();
            $cate_id = 39;
            $map['cate_id'] = $cate_id;
            $field = "*";
            $order = "add_time desc";
            $news = D('NewsHouse')->get_all($map,$field,$order);
            $new_count = D('NewsHouse')->count($map);
        //    print_r($new_count);
            foreach($news as $k => $v) {
                $news[$k]['notice_date'] = date('Y-m-d',$v['add_time']);
                $news[$k]['notice_time'] = date('H:i:s',$v['add_time']);
                $news_id[] = $v['id'];
                $news[$k]['nickname'] = $user['nickname'];
            }
            $map = array();
            $map['user_id'] = $user_id;
            $new_notice = D('NewsNotice')->where($map)->select();
            $notice_new = D('NewsNotice')->where($map)->count();
        //    print_r($notice_new);
            foreach ($news as $k => $v) {
                $id = $v['id'];
                foreach ($new_notice as $ke => $va) {
                    $news_id = $va['news_id'];
                    if($news_id==$id){
                        $news[$k]['is_read'] = 1;
                        break;
                    }else{
                        $news[$k]['is_read'] = 0;
                    }
                }
            }
            $notice_read = $new_count-$notice_new;
            if($notice_read>0) {
                //存在未读
                $notice_reads = 1;
            } else {
                $notice_reads = 0;
            }

        //   print_r($notice_reads);
            //站内信
            $map = array();
            $map['user_id'] = $user_id;
            $mes = D('Message')->get_all($map);
            $map = array();
            $map['id'] = $user_id;
            $user = D('User')->get_one($map);
            $map = array();
            foreach($mes as $k => $v) {
                $map['id'] = $v['type_id'];
                $mes_type = D('MessageType')->get_one($map);
				if($mes_type['type']) {
                $mes[$k]['type_id'] = $mes_type['type'];
				} else {
				$mes[$k]['type_id'] = '系统通知';
				}
            //    $mes[$k]['content'] = substr($v['content'],0,20).'....';
                $mes[$k]['notice_date'] = date('Y-m-d',$v['add_time']);
                $mes[$k]['notice_time'] = date('H:i:s',$v['add_time']);
                if($v['is_read']==0) {
                    $unread[] = $v['is_read'];
                }
                $mes[$k]['nickname'] = $user['nickname'];
            }

            $this->assign('notice_reads',$notice_reads);
            $this->assign('mes',$mes);
            $this->assign('unread',$unread);
            $this->assign('news',$news);
            $this->display();
        } else {
            header("location:/Home/Index/index");
        }
    }


    //公告详情
    public function notice_details() {
        $user_id = $_SESSION['user']['id'];
        if($user_id) {
            $news_id = I('get.news_id');
            $map['id'] = $news_id;
            $news = D('NewsHouse')->get_one($map);
            $news['notice_date'] = date('Y-m-d',$news['add_time']);
            $news['notice_time'] = date('H:i:s',$news['add_time']);
            $map = array();
            $map['id'] = $user_id;
            $user = D('User')->get_one($map);
            $news['nickname'] = $user['nickname'];
            if($news) {
                $data['news_id'] = $news['id'];
                $data['user_id'] = $user_id;
                $map = array();
                $map['news_id'] = $news['id'];
                $map['user_id'] = $user_id;
                $newnotice = M('NewsNotice')->where($map)->find();
                if(!$newnotice){
                    M('NewsNotice')->add($data);
                }
            }
        //    print_r($news);
            $this->assign('news',$news);
            $this->display();
        } else {
            header("location:/Home/Index/index");
        }
    }
    //公告详情
    public function notice_message() {
        $user_id = $_SESSION['user']['id'];
        if($user_id) {
            $mes_id = I('get.mes_id');
            $map['id'] = $mes_id;
            $news = D('Message')->get_one($map);
            $news['notice_date'] = date('Y-m-d',$news['add_time']);
            $news['notice_time'] = date('H:i:s',$news['add_time']);
            $map = array();
            $map['id'] = $user_id;
            $user = D('User')->get_one($map);
            $news['nickname'] = $user['nickname'];
            $map = array();
            $map['user_id'] = $user_id;
            $map['id'] = $mes_id;
            if($news) {
                $data['is_read'] = 1;
                D('Message')->edit($map,$data);
            }

        //    print_r($news);
            $this->assign('news',$news);
            $this->display();
        } else {
            header("location:/Home/Index/index");
        }
    }


    //账单明细
    public function trade_details() {
        $user_id = $_SESSION['user']['id'];
        if($user_id){
            $map['user_id'] = $user_id;
            $bill = D('Bill')->get_all($map,$order='id desc');
            foreach($bill as $k => $v) {
                $bill[$k]['notice_date'] = date('Y-m-d',$v['add_time']);
                $bill[$k]['notice_time'] = date('H:i:s',$v['add_time']);
                if($v['type']==1) {
                    $bill[$k]['type'] = "首投返利（用户）";
                } elseif($v['type']==2) {
                    $bill[$k]['type'] = "加息返利（用户）";
                } elseif($v['type']==3) {
                    $bill[$k]['type'] = "首投返利（推手）";
                } elseif($v['type']==4) {
                    $bill[$k]['type'] = "加息返利（推手）";
                } elseif($v['status']==1) {
                    $bill[$k]['type'] = "用户提现";
                }
            }
        //    print_r($bill);
            $this->assign('bill',$bill);
            $this->display();
        } else {
            header("location:/Home/Index/index");
        }
    }

    //账单详情
    public function trade_details_info() {
        $user_id = $_SESSION['user']['id'];
        if($user_id){
            $id = I('get.id');
            $map['id'] = $id;
            $bill = D('Bill')->get_one($map);
                if($bill['type']==1) {
                    $bill['type'] = "首投返利（用户）";
                } elseif($bill['type']==2) {
                    $bill['type'] = "加息返利（用户）";
                } elseif($bill['type']==3) {
                    $bill['type'] = "首投返利（推手）";
                } elseif($bill['type']==4) {
                    $bill['type'] = "加息返利（推手）";
                } elseif($bill['status']==1) {
                    $bill['type'] = "用户提现";
                }
                $bill['add_time'] = date('Y-m-d H:i:s',$bill['add_time']);
                $fl_user_id = $bill['fl_user_id'];
                $map = array();
                if($fl_user_id){
                    $map['id'] = $fl_user_id;
                    $fl = D('User')->get_one($map);
                }
                $bill['remark'] =$fl['nickname'].$bill['type'];
               // print_r($bill);
            $this->assign('bill',$bill);
            $this->display();
        } else {
            header("location:/Home/Index/index");
        }
    }



 /*派投资红包*/

    function send_pack(){
         //推荐平台
        $map = array();
        $map['status']=0;
        $platform_list= D('Platform')->get_all($map);
        foreach ($platform_list as $k => $v) {
            $map = '';
            $map['platform_id'] = $v['id'];
            $platform_scheme = D('PlatformScheme')->where($map)->select();
            //查询红包价格
            foreach ($platform_scheme as $ke => $va) {
               $scheme_id = $va['id'];
               $map = array();
               $map['user_id'] = $_SESSION['user']['id'];
               $map['scheme_id'] = $scheme_id;
               $redpacket = D('RedPacket')->get_one($map);
               if($redpacket){
                    $platform_scheme[$ke]['p_user_award'] = $redpacket['user_award'];
                    $platform_scheme[$ke]['p_push_award'] = $redpacket['push_award'];
               }else{
                    $platform_scheme[$ke]['p_user_award'] = $va['user_award'];
                    $platform_scheme[$ke]['p_push_award'] = $va['push_award'];
               }
            }
            $platform_list[$k]['scheme'] = $platform_scheme;
        }
        $user = $_SESSION['user']['id'];
        $this->assign('user',$user);
        $this->assign('platform_list',$platform_list);
        $this->display();
    }

    /*我的观点*/
    public function viewpoint(){
        $this->display();
    }
    /*师傅的观点*/
    public function master_dynamic(){
        $this->display();
    }


     //提现
    public function cash(){

        $user_id = $_SESSION['user']['id'];
        if($user_id){
            $val = I('get.val');
            $map['id'] = $user_id;
            $user_info = D('User')->get_one($map);
            $user_info['bank_card_small'] = substr($user_info['bank_card'],-4);

            $this->assign('user_info',$user_info);
            $this->assign('val',$val);
        }

        $this->display();
    }

    public function my_info_dynamic(){
        $val = I('get.val');
        $user_id = I('get.user_id');
        $page = 1;
        $page = $page-1;;
        $pagenum =5; 
        $where['id'] = $user_id;
        $user_info = D('user')->get_one($where);
        if($val==1){
            //短评
            $map['user_id'] = $user_id;
            $list = D('PlatformCommentary')-> get_all_page($map,$page,$pagenum,$order="add_time desc");
            foreach ($list as $k => $v) {
                $list[$k]['add_date'] = date('Y-m-d H:i:s',$v['add_time']);
                $list[$k]['tag'] =explode(',',$v['tag']);
                $list[$k]['type'] = 6;
            }
        }elseif($val==2){
            //深度
                $map['user_id'] = $user_id;
                $list = D('Depth')->get_all_page($map,$page,$pagenum,$order="add_time desc");
                foreach ($list as $k => $v) {
                    $list[$k]['add_date'] = date('Y-m-d H:i:s',$v['add_time']);
                    $list[$k]['tag'] =explode(',',$v['tag']);
                    $list[$k]['type'] = 7;
                }
        }elseif($val==3){
            //信批
                $map['user_id'] = $user_id;
                $list = D('CircleAnnounce')->get_all_page($map,$page,$pagenum,$order="add_time desc");
                foreach ($list as $k => $v) {
                    $list[$k]['add_date'] = date('Y-m-d H:i:s',$v['add_time']);
                    $list[$k]['tag'] =explode(',',$v['tag']);
                    $list[$k]['type'] = 9;
                }
        }elseif($val==4){
            //信批
                $map['user_id'] = $user_id;
                $list = D('CircleOpinion')->get_all_page($map,$page,$pagenum,$order="add_time desc");
                foreach ($list as $k => $v) {
                    $list[$k]['add_date'] = date('Y-m-d H:i:s',$v['add_time']);
                    $list[$k]['tag'] =explode(',',$v['tag']);
                    $list[$k]['type'] = 8;
                }
        }


        //合并数据
        foreach ($list as $k => $v) {
                //合并平台名称
                $platform_id = $v['platform_id'];
                $map['id'] = $platform_id;
                $platform_info = D('Platform')->get_one($map);
                $list[$k]['platform_name'] = $platform_info['name'];

                //合并评价
                $map = array();
                $map['user_id'] = $user_id;
                $map['platform_id'] = $platform_id;
                $list[$k]['grade'] = D('PlatformGrade')->get_one($map);

                //合并点赞
                $where = array();
                if($_SESSION['user']['id']){
                    $where['id_user'] = $_SESSION['user']['id'];
                    $where['type'] =$v['type'];
                    $where['id_target'] = $v['id'];
                    $praise = D('Praise')->get_one($where);
                    if($praise){
                         $list[$k]['is_praise'] = 1;
                    }else{
                         $list[$k]['is_praise'] = 0;
                    }
                }else{
                     $list[$k]['is_praise'] = 0;
                }
        }
       // print_r($list);
        $this->assign('user_info',$user_info);
        $this->assign('list',$list);
        $this->display();
    }


        //获取手续费
    public function get_charge(){
            $firstday = strtotime(date('Y-m-d'),time());
            $lastday = strtotime(date('Y-m-d',strtotime('+1 day')));
            $map['add_time']=array('between',"$firstday,$lastday");
            $map['user_id'] = I('post.id');
            $num = D('Cash')->where($map)->count();
            //echo D('Cash')->count();
            if($num>=1) {
                $data = array('err'=>1,'msg'=>"1");
            }else{
                $data = array('err'=>0,'msg'=>"0");
            }
            $this->ajaxReturn($data);
    }

     //提现
    public function withdrawals() {
        if(IS_POST) {
            //    校验用户是否实名认证
            $user = $_SESSION['user'];
            $id = $user['id'];
            $map['id'] = $id;
            $u = D('User')->get_one($map);
            //验证绑定身份证
            if($u['card_status']==1 && !empty($u['idcard'])) {
            //校验用户是否绑定银行卡
                if(!empty($u['bank_card'])) {
                //判断提现金大于50元才能提
                    $affirmdepo = I('post.affirmdepo');
                    $type = I('post.type');
                    if(is_numeric($affirmdepo)) {
                        //判断是否为推手
                        if($type==2) {
                                if($affirmdepo>=50) {
                                    if($affirmdepo<=$u['prize_balance']) {
                                            $data =  $this->cash_do($id,$affirmdepo,$type);
                                    } else {
                                            $data = array('err'=>4,'msg'=>"您的可提金额不足，前往投资拿更多返利");
                                    }
                                } else {
                                    $data = array('err'=>3,'msg'=>"提现金额需大于50元");
                                }
                        //不是推手1元起提
                        } else {
                                if($affirmdepo>1) {

                                    if($affirmdepo<=$u['balance']) {
                                         $data = $this->cash_do($id,$affirmdepo,$type);
                                    } else {
                                            $data = array('err'=>4,'msg'=>"您的可提金额不足，前往投资拿更多返利");
                                    }
                                } else {
                                    $data = array('err'=>3,'msg'=>"提现金额需大于1元");
                                }

                        }


                    } else {
                        $data = array('err'=>9,'msg'=>"请输入提现金额");
                    }
                 } else {
                     $data = array('err'=>2,'msg'=>"您还未绑定银行卡，请前往绑定银行卡！");
                 }
            } else {
                $data = array('err'=>1,'msg'=>"完成实名认证后就可以提现哦，请前往实名认证！");
            }
        }
        $this->ajaxReturn($data);
    }

    //提现操作
    public function cash_do($id,$affirmdepo,$type){
        //判断一天一次免手续费
        $map = array();
        $firstday = strtotime(date('Y-m-d'),time());
        $lastday = strtotime(date('Y-m-d',strtotime('+1 day')));
        $map['add_time']=array('between',"$firstday,$lastday");
        $map['user_id'] = $id;
        $num = D('Cash')->where($map)->count();
        if($num>=1) {
            $deduction = 1;
            $data = array('err'=>6,'msg'=>"提现收取手续费1元");
        } else{
            $deduction = 0;
            $data = array('err'=>5,'msg'=>"每天首次提现免手续费");
        }
        $sursums = $affirmdepo-$deduction;//已扣除手续费
        $datas['user_id'] = $id;
        $datas['amount'] = $sursums;//提现金额
        $datas['fee'] = $deduction;//手续费
        $datas['add_time'] = time();//添加时间
        $res = D('Cash')->add($datas);
        if($res) {
            $data = array('err'=>0,'msg'=>"发起提现");
            $dat['user_id'] = $id;
            $dat['quota'] = $sursums;
            $dat['deduction'] = $deduction;
            $dat['add_time'] = time();//添加时间
            $dat['status'] = 1;
            $dat['remarks'] = "用户发起资金提现";
            $map = array();
            $map['id'] = $id;
            $user_info = D('User')->get_one($map);

            if($type==1){
                $balance = $user_info['balance'];
            }else{
                $balance = $user_info['prize_balance'];
            }

            $quota = $sursums+$deduction;

            if($type==1){
                D('User')->balance($map,$quota);
            }else{
                D('User')->prize_balance($map,$quota);
            }

            $balance =$balance-$quota;
            $dat['balance'] = $balance;
            $dat['tx_type'] = $type;
            $ress = D('Bill')->add($dat);
            $phone = $u['user_phone'];
            $date = date('Y年m月d日 H时i分',time());
            $money = $sursums+$deduction;
            $send = R('Send/cash_apply',array($phone,$date,$money,$deduction));
            R('Msg/sendmail',array($typename='affirmdepo',$userid = $id));
        }else {
            $data = array('err'=>8,'msg'=>"提现失败，参数有误");
            
        }
        return $data;
        

    }

    //实名认证
    public function relaname(){
        if(IS_POST){

        }else{
            $this->display();
        }
    }

    //綁定銀行卡
    public function bind_bank_card(){
        $_SESSION['user']['bank_card']='';
        $user_id = $_SESSION['user']['id'];
        $map['id'] = $user_id;
        $user_info = D('User')->get_one($map);
        $user_info['realname'] = '**'.mb_substr($user_info['realname'],-1,1,'utf8');
        $user_info['idcard'] = substr($user_info['idcard'],0,4).'********'.substr($user_info['idcard'],-4);
        $this->assign('user_info',$user_info);
        $this->display();
    }

        //綁定銀行卡
    public function unbind_bank_card(){
        $_SESSION['user']['bank_card']='';
        $user_id = $_SESSION['user']['id'];
        $map['id'] = $user_id;
        $user_info = D('User')->get_one($map);
        $user_info['realname_m'] = '**'.mb_substr($user_info['realname'],-1,1,'utf8');
        $user_info['idcard_m'] = substr($user_info['idcard'],0,4).'********'.substr($user_info['idcard'],-4);
        $user_info['bank_card_m'] = substr($user_info['bank_card'],0,4).'********'.substr($user_info['bank_card'],-4);
       // print_r($user_info);die;
        $this->assign('user_info',$user_info);
        $this->display();
    }

    //解绑银行卡第一步
    //輸入身份證驗證
    public function unbind_bank_first(){
        if(IS_POST){
            $user_id = $_SESSION['user']['id'];
            $idcard = I('post.idcard');
            $map['id'] = $user_id;
            $map['idcard'] = $idcard;
            $r = D('User')->get_one($map);
            if($r){
                $_SESSION['is_idcard']=1;
                $data=array('err'=>0);
            }else{
                $data=array('err'=>1);
            }
            $this->ajaxReturn($data);

        }else{
            $this->display();
        }
    }

    //解绑银行卡第二步
    //輸入驗證碼
    public function unbind_bank_end(){
        if($_SESSION['is_idcard']){
            $user_id = $_SESSION['user']['id'];
            $map['id'] = $user_id;
            $r = D('User')->get_one($map);
            if(IS_POST){
                $user_id = $_SESSION['user']['id'];
                $map['id'] = $user_id;
                $r = D('User')->get_one($map);
                $phone = $r['user_phone'];
                $map = array();
                $startday = strtotime(date('Y-m-d 0:0:0',time()));
                $map['add_time'] = array('gt',$startday);
                $map['phone'] = $phone;
                $msgcount = C('MSG_COUNT_USER');
                $ip = get_client_ip_user();
                $count = D("Verification")->count($map);
                if($count>=$msgcount){
                    $data = array('err'=>2,'msg'=>"验证码发送次数过多,请明天再试！");
                    $this->ajaxReturn($data);
                }else{
                    $map = array();
                    $map['phone'] = $phone;
                    $map['status'] =0;
                    $map['role'] =6;
                    $r = D("Verification")->get_one($map);
                    if($r['add_time']<=(time()-60)){
                        //发送验证码
                        $code = rand(111111,999999);
                        $res = R('Send/sendsms',array($phone,$code,$type = 1));
                        // print_r($res);die;
                        //$res = array('err'=>0);
                        if($res['err']==0){
                            //记录短信发送
                            $msgdata['type']=1;
                            $msgdata['role']=6;
                            $msgdata['phone']=$phone;
                            $msgdata['status']=0;
                            $msgdata['add_time']=time();
                            $msgdata['time_failure']=time()+300;
                            $msgdata['code']=$code;
                            $msgdata['ip']=$ip;
                            $res = D("Verification")->add($msgdata);
                                if($res){
                                    unset($_SESSION['is_idcard']);
                                    $data = array('err'=>0,'msg'=>"发送成功！");
                                    $this->ajaxReturn($data);
                                }else{
                                    $data = array('err'=>5,'msg'=>"发送失败！");
                                    $this->ajaxReturn($data);
                                }
                        }else{
                                $data = array('err'=>6,'msg'=>"发送失败，接口异常！");
                                $this->ajaxReturn($data);
                        }
                    }else{
                        $data = array('err'=>4,'msg'=>"发送频率过快，休息一会！");
                        $this->ajaxReturn($data);
                    }
                }
            }
            

            $this->assign('user_info',$r);
            $this->display();
        }else{
            header('location:/');
        }
        
    }
    //銀行卡解綁
    public function un_bind_bank(){

        $userid = $_SESSION['user']['id'];
        $map['id'] = $userid;
        $r = D('User')->get_one($map);
        $phone = $r['user_phone'];
        $map = array();
        $map['phone'] = $phone;
        $map['status'] =0;
        $map['role'] =6;
        $v_msg= D("Verification")->get_one($map);
        if($v_msg){
            $code = I('post.code');
            $time_failure = $v_msg['time_failure'];
            if($time_failure<time()){
                $data = array('err'=>2,'msg'=>"到验证码已过期！");
            }else{
                $v_code = $v_msg['code'];
                if($code==$v_code){
                    //解绑
                    $datas['bank_card'] = '';
                    $map = array();
                    $map['id'] = $userid;
                    $r = D('User')->edit($map,$datas);
                    $map = array();
                    $map['phone'] = $phone;
                    $arr['status'] = 1;
                    $res = D('Verification')->edit($map,$arr);
                    if($r){
                         $data = array('err'=>0,'msg'=>"解绑成功");
                    }else{
                         $data = array('err'=>4,'msg'=>"解绑失败");
                    }
                }else{
                    $data = array('err'=>3,'msg'=>"验证码错误");
                }
            }
        }else{
            $data = array('err'=>1,'msg'=>"未找到验证码！");
        }
        $this->ajaxReturn($data);

    }

    //绑定银行卡
    public function bank() {
        $postdata = I('post.');
        $user = $_SESSION['user'];
        if($user['bank_card']){
            $data = array('err'=>3,'msg'=>"您已绑定银行卡，请解绑后再试");
             $this->ajaxReturn($data);
        }
        if($user['card_status']==0) {
            $data = array('err'=>3,'msg'=>"未实名认证，请先实名验证");
        } else {
            $s = trim($postdata['bank_card']);
            $bank_phone = trim($postdata['bank_phone']);
                //验证手机号
            if(strlen($bank_phone)!=11 || !preg_match( '/^1[3|4|5|7|8][0-9]\d{4,8}$/',$bank_phone)) {
                $data = array('err'=>5,'msg'=>"手机号有误");
            } else {
                // $luhm = $this->luhm($s);
                $luhm['err']=0;
                if($luhm['err']==0) {
                    $time = time();
                    $today = date('Y-m-d',$time);
                    $firstday = strtotime($today);
                    $tomorrow = date('Y-m-d',strtotime( "+1 day",$time));
                    $lastday = strtotime($tomorrow);
                    $map['user_id'] = $user['id'];
                    $map['add_time'] = array('between',"$firstday,$lastday");
                    $bank_info = M('Bank')->where($map)->count();
                    if($bank_info<5) {
                            //绑定银行卡
                        $bank =  R('Bankcard/get_card_info',array($user['realname'],$user['idcard'],$s,$bank_phone));

                        //测试数据  : 王华斌    362427199205230819    6212264000004496036    18079792115
                        //   $bank =  R('Bankcard/get_card_info',array("王华斌","362427199205230819","6212264000004496036","15099835829"));
                        //   $bank = Array(
                        //            'code' => 10000,
                        //            'message' => '成功',
                        //            'data' => Array
                        //                (
                        //                    'state' => 1
                        //                ),
//
                         //           'seqNo' => 'OT2379551708151459'
                         //           );
                        //    print_r($bank);
                        $ba['code'] = $bank['code'];
                        $ba['message'] = $bank['message'];
                        $ba['state'] = $bank['data']['state'];
                        $ba['seqno'] = $bank['seqNo'];
                        $ba['add_time'] = time();
                        $ba['user_id'] = $user['id'];
                        $ban = M('Bank')->add($ba);
                        //调用接口成功时做判断
                            if($bank['code']==10000 && $bank['data']['state']==1) {
                                $map['id'] = $user['id'];
                                $data['bank_card'] = $postdata['bank_card'];
                                $res = D('User')->edit($map,$data);
                                if($res) {
                                    $_SESSION['user']['bank_card'] = $data['bank_card'];
                                    R('Msg/sendmail',array($typename='bind_bank',$userid = $user['id']));
                                    $data = array('err'=>0,'msg'=>"添加银行卡成功");
                                } else {
                                    $data = array('err'=>1,'msg'=>"添加银行卡失败");
                                }
                            } else {
                                $data = array('err'=>4,'msg'=>"银行卡信息有误");
                            }
                    } else {
                        $data = array('err'=>6,'msg'=>"一天只限绑定5次银行卡");
                    }

                } else {
                    $data = array('err'=>2,'msg'=>"银行卡填写错误");
                }
            }

        }
        $this->ajaxReturn($data);
    }


    //自动提现开关
    public function auto_cash(){
        $auto = I('post.auto');
        if($auto==1){
            $bank_card = $_SESSION['user']['bank_card'];
            if($bank_card){
                $map=array();
                $map['id'] = $_SESSION['user']['id'];
                $data['auto_cash'] = 1;
                $r = D('User')->edit($map,$data);
                if($r){
                    $data = array('err'=>0,'msg'=>"自动提现已开启！");
                }else{
                    $data = array('err'=>1,'msg'=>"自动提现开启失败！");
                }
            }else{
                $data = array('err'=>2,'msg'=>"您还未绑定银行卡正在前往安全中心！");
            }
        }else{
            $map=array();
            $map['id'] = $_SESSION['user']['id'];
            $data['auto_cash'] = 0;
            $r = D('User')->edit($map,$data);
            if($r){
                $data = array('err'=>0,'msg'=>"自动提现已关闭！");
            }else{
                $data = array('err'=>1,'msg'=>"自动提现关闭失败！");
            }
        }
        $this->ajaxReturn($data);
    }

    //用戶個人中心設置
    public function setting(){
        $userid = $_SESSION['user']['id'];
        $map['id'] = $userid;
        $user = D('User')->get_one($map);
        $user['realname_m'] = '**'.mb_substr($user['realname'],-1,1,'utf8');
        $this->assign('user',$user);
        $this->display();
    }
    //修改用戶暱稱
    public function change_nickname(){
        if(IS_POST){
            $nickname = I('post.nickname');
            $map['id'] = $_SESSION['user']['id'];
            $data['nickname'] = $nickname;
            $r = D('User')->edit($map,$data);
            if($r){
                    $_SESSION['user']['nickname'] = $nickname;
                    $data = array('err'=>0,'msg'=>"修改成功！");
            }else{
                    $data = array('err'=>1,'msg'=>"修改失败！");
            }
                $this->ajaxReturn($data);

        }else{
            $this->display();
        }
    }

    //修改登錄密碼
    public function change_password(){
        if(IS_POST){

            $new_password = I('post.new_password');
            $old_password = I('post.old_password');
            $userid = $_SESSION['user']['id'];
            $map['id'] = $userid;
            $user_info = D('User')->get_one($map);
            $user_mixs = $user_info['user_mixs'];
            
            $old_password = md5($old_password.$user_mixs);
            if($old_password == $user_info['user_pass']){

                $data['user_pass'] = md5($new_password.$user_mixs);
                $r = D('User')->edit($map,$data);
                if($r){
                    $data = array('err'=>0,'msg'=>"修改成功");
                }else{
                    $data = array('err'=>2,'msg'=>"修改失败");
                }


            }else{
                $data = array('err'=>1,'msg'=>"原密码错误！");
            }
            $this->ajaxReturn($data);
            

            $r = D('User')->get_one();
        }else{
            $this->display();
        }
    }
      //修改手機號碼
    public function change_phone(){
        if(IS_POST){
            $code = I('post.code');
            $map['phone'] = $_SESSION['user']['user_phone'];
            $map['status'] = 0;
            $map['role'] = 3;
            $codeinfo = D('Verification')->get_one($map);

            if($codeinfo){
                if($codeinfo['code']==$code && $codeinfo['status']==0){
                    if($codeinfo['time_failure']<=time()){
                        $data = array('err'=>1,'msg'=>'验证码已过期！');
                    }else{
                        //验证码有效
                        $_SESSION['change_phone'] = $phone;
                        $_SESSION['code_id'] = $codeinfo['id'];
                        //跳转到密码设置页面
                        $data = array('err'=>0);
                    }
                }else{
                    $data = array('err'=>3,'msg'=>'验证码错误！');
                }
            }else{
                    $data = array('err'=>2,'msg'=>'验证码无效！');
            }

            $this->ajaxReturn($data);
        }else{
            $userid = $_SESSION['user']['id'];
            $map['id'] = $userid;
            $user = D('User')->get_one($map);
            $this->assign('user',$user);
            $this->display();
        }
    }
    //提交新手机号码完成手机号码修改
    public function change_phone_end(){
       if(IS_POST){
        $phone = I('post.phone');
        $code = I('post.code');

        $old_phone = $_SESSION['change_phone'];
        $code_id = $_SESSION['code_id'] ;
        $map['status'] = 0;
        $map['phone'] = $phone;
        $map['role'] =3;
        $codeinfo = D('Verification')->get_one($map);
        if($codeinfo){
                if($codeinfo['code']==$code && $codeinfo['status']==0){
                    if($codeinfo['time_failure']<=time()){
                        $data = array('err'=>1,'msg'=>'验证码已过期！');
                    }else{
                        //验证码正确
                    $map = array();
                    $data = array();
                    $map['id'] = $_SESSION['user']['id'];
                    $data['user_phone'] = $phone;
                    $r = D('User')->edit($map,$data);

                    if($r){                       
                        $map = array();
                        $map['id'] = $code_id;
                        $data_arr['status'] = 1;
                        $data = D("Verification")->edit($map,$data_arr);
                        $_SESSION['user']['user_phone'] = $phone;
                        $data = array('err'=>0,'msg'=>'成功！');
                        unset($_SESSION['change_phone']);
                        unset($_SESSION['code_id']);
                    }else{
                        $data = array('err'=>1,'msg'=>'失败！');
                    }                    

                    }
                }else{
                    $data = array('err'=>3,'msg'=>'验证码错误！');
                }
            }else{
                    $data = array('err'=>2,'msg'=>'验证码无效！');
            }
            $this->ajaxReturn($data);

        }else{
        $this->display();

        }
    }



    //全部明细
    public function invest_list() {
        $val = I('post.val');
        $user_id = $_SESSION['user']['id'];
        $map['user_id'] = $user_id;
        if($val==1) {
            $map['type']  = array('in','1,3');
        } elseif($val==2) {
            $map['type']  = array('in','2,4');
        } elseif($val==3) {
            $map['type'] = 5;
            $map['status'] = 1;
        }
        $bill = D('Bill')->get_all($map);
            foreach($bill as $k => $v) {
                $bill[$k]['notice_date'] = date('Y-m-d',$v['add_time']);
                $bill[$k]['notice_time'] = date('H:i:s',$v['add_time']);
                if($v['type']==1) {
                    $bill[$k]['type'] = "首投返利（用户）";
                } elseif($v['type']==2) {
                    $bill[$k]['type'] = "加息返利（用户）";
                } elseif($v['type']==3) {
                    $bill[$k]['type'] = "首投返利（推手）";
                } elseif($v['type']==4) {
                    $bill[$k]['type'] = "加息返利（推手）";
                } elseif($v['status']==1) {
                    $bill[$k]['type'] = "用户提现";
                }
            }
        $this->assign('bill',$bill);
        $content = $this->fetch('invest_list');
        $this->ajaxReturn(array('content'=>$content));
    }


    //我的投資人
    
    public function investor(){
        if(!$_SESSION['user']['id']){
            header('Location:/');die;
        }
        $map['referral'] = $_SESSION['user']['id'];
        $user_count =  D('User')->count($map); //总推广用户数
        $user_push_list =  D('User')->get_all($map); //总推广用户数
        foreach($user_push_list as $k=>$v){
            $user_id_push[] = $v['id'];
        }

        $user_id_push_count = count($user_id_push);
        if($user_id_push){
             $map = array();
             $map['user_id'] = array('in',$user_id_push);
             $u_obj = D('UserObject')->group_count($map); //投资用户

              $u_list = D('UserObject')->get_all_group($map,$group="user_id",$field='*',$page=1,$pagenum=10,$order='id desc'); //投资用户
             foreach($u_list as $k=>$v){
                $u_list_id[] = $v['user_id'];
             }
            if($u_list){
                foreach($u_list as $k=>$v){
                    $u_list_id[] = $v['user_id'];
                }
                 if($u_list_id){
                    $map['id'] = array('not in',$u_list_id);
                    $map['referral'] =$_SESSION['user']['id'];
					$order='id desc';
                    $un_obj = D('User')->where($map)->order($order)->select(); //投资用户

                 }else{
                    $un_obj ='';
                 }
             }else{
                $map['id'] = array('in',$user_id_push);
                $map['referral'] =$_SESSION['user']['id'];
                $un_obj = D('User')->where($map)->select(); //投资用户
             }
              
             foreach ($un_obj as $k => $v) {
                $map = array();
                $map['user_id'] = $v['id'];
                $userplat = D('UserPlatform')->where($map)->find(); //投资用户
                $un_obj[$k]['userplat'] = $userplat;

                $map = array();
                $map['id_user'] =$v['id'];
                $user_info_detail= D('UserInfo')->get_one($map);
                $un_obj[$k]['user_info_detail'] = $user_info_detail;
                if($userplat){
                    $t_s = $user_info_detail['add_time'] - $userplat['add_time'];
                    if($t_s<10){
                        $un_obj[$k]['is_plat_reg'] = 1;
                    }else{
                         $un_obj[$k]['is_plat_reg'] = 0;
                    }
                }else{
                         $un_obj[$k]['is_plat_reg'] = 0;
                }
                
                $map = array(); 
                $map['id'] =$userplat['platform_id'];
                $platform_info = D('Platform')->get_one($map);
                $un_obj[$k]['platform_name'] = $platform_info['name'];

             }
             $u_obj_count_num = count($u_obj); //已投资用户总数
             $u_obj_count = count($u_obj); //已投资用户总数
             $u_obj_count = ceil($u_obj_count/10);
             $u_obj_count?$u_obj_count:0;
             $un_obj_count =count($un_obj);//未投资用户总数
             $un_obj_count?$un_obj_count:0;

             foreach($u_list as $k=>$v){
                $user_id = $v['user_id'];
                $map = array();
                $map['user_id'] = $user_id;
                $u_list[$k]['invest_num'] = D('UserObject')->obj_sum($map);  //累计投资
                //echo D('UserObject')->_sql();
                // 用户信息
                $map = array();
                $map['id'] = $user_id;
                $u_list[$k]['user_info'] = D('User')->get_one($map);
                $map = array();
                $map['id_user'] = $user_id;
                $u_list[$k]['user_info_detail'] = D('UserInfo')->get_one($map);
                $map = array();
                $map['id'] =$v['platform_id'];
                $platform_info = D('Platform')->get_one($map);
                $u_list[$k]['platform_name'] = $platform_info['name'];
                //用户首投加息
                $map = array();
                $map['user_id'] = $user_id;
                $map['type'] = 1;
                $u_list[$k]['user_st'] = D('Bill')->m_sum($map);

                //用户复投加息
                $map = array();
                $map['user_id'] = $user_id;
                $map['type'] = 2;
                $u_list[$k]['user_ft'] = D('Bill')->m_sum($map);


                 //推手首投加息
                $map = array();
                $map['fl_user_id'] = $user_id;
                $map['type'] = 3;
                $u_list[$k]['push_st'] = D('Bill')->m_sum($map);

                //推手复投加息
                $map = array();
                $map['fl_user_id'] = $user_id;
                $map['type'] =4;
                $u_list[$k]['push_ft'] = D('Bill')->m_sum($map);

            }
            $hz_plat_info = $this->hz_platinfo($_SESSION['user']['id']);
         }
         //已合作平台
         $this->assign('hz_plat_info',$hz_plat_info);
         $this->assign('u_obj_count_num',$u_obj_count_num);
         $this->assign('un_obj_count',$un_obj_count);
         $this->assign('u_obj_count',$u_obj_count);
         $this->assign('u_list',$u_list);
         $this->assign('un_obj',$un_obj);
        $this->display();
    }
    
    //投資人明細
    public function investor_detail(){
        $data = I('get.data');
        $val = I('get.val');
        $data = unserialize(base64_decode($data));
        $data['tz_plat'] = urldecode($data['tz_plat']);
        $data['reg_plat'] = urldecode($data['reg_plat']);
        $this->assign('val',$val);
        $this->assign('data',$data);
        $this->display();
    }

    //見習推手任務頁
    public function push_task(){
        $this->display();
    }

    //提现成功页
    public function cash_success(){
        $this->display();
    }
    //提现是錯誤页
    public function cash_error(){
        $this->display();
    }
    //提现說明
    public function cash_explain(){
        $this->display();
    }
    //實名成功页
    public function realname_success(){
        $user_id = $_SESSION['user']['id'];
        $map['id'] = $user_id;
        $user_info = D('User')->get_one($map);
        $user_info['realname'] = '**'.mb_substr($user_info['realname'],-1,1,'utf8');
        $user_info['idcard'] = substr($user_info['idcard'],0,4).'********'.substr($user_info['idcard'],-4);
        $this->assign('user_info',$user_info);
        $this->display();
    } 
    //提现是錯誤页
    public function bank_error(){
        $this->display();
    }
     //實名成功页
    public function bank_success(){
        $user_id = $_SESSION['user']['id'];
        $map['id'] = $user_id;
        $user_info = D('User')->get_one($map);
        $user_info['realname'] = '**'.mb_substr($user_info['realname'],-1,1,'utf8');
        $user_info['idcard'] = substr($user_info['idcard'],0,4).'********'.substr($user_info['idcard'],-4);
        $this->assign('user_info',$user_info);
        $this->display();
    }
    //解綁銀行卡成功
    public function unbind_bank_success(){
        $this->display();
    }
    //解綁銀行可失敗
    public function unbind_bank_error(){
        $this->display();
    }


    //實名錯誤页
    public function realname_error(){
        $this->display();
    }
    



    //头像修改
    public function change_head_pic(){
            $user_pic = I('post.file');
            $userid = $_SESSION['user']['id'];
            if($user_pic){
                 $res = R('Upload/index',array($user_pic));
                 $data['url_img'] = $res['url'];
            }
        //    print_r($data);
            $map['id'] =$userid;
             $r = D('User')->edit($map,$data);
            if($r){
                $this->ajaxReturn(array('errorCode'=>1,'msg'=>"修改成功","url"=>$res['url']));
            }else{
                 $this->ajaxReturn(array('errorCode'=>0,'msg'=>"修改失败"));
            }
        }

 /**
     * [center 账户中心]
     * @return [type] [description]
     */
    public function center(){
        $userid = $_SESSION['user']['id'];
            $id = I('get.id');
            if($userid && $id){
                if($userid == $id){
                //主
                    $is_self = 1;
                }else{
                //客
                    $is_self = 0;
                    //判断身份
                }

            }elseif($id && !$userid){
                //客
                $is_self = 0;
            }elseif(!$userid && !$id){
                //无效访问
                 header("location:/Home/User/login");
            }else{
               //主
               $is_self = 2;
            }

        if($is_self){
            if($userid){
                if(!$id){
                    $id = $userid;
                }
                $map['id'] = $id;
                $u_r = D('User')->get_one($map);
               //print_r($u_r);
                $role = $u_r['role'];
                if($role==2){
                    $map = array();
                    $map['user_id'] = $u_r['id'];
                    $r_info = D('Recommend')->get_one($map);
                    $recommend_id = $r_info['id'];
                    $p_list = $this->get_push_plat($recommend_id);
                    $packet = R('Packet/get_redpacket',array('userid'=>$u_r['id'],'platform_id'=>''));

                      //统计徒弟数量
                    $where = array();
                    $where['referral'] = $u_r['id'];
                    $r_info['fans_count']=D('User')->count($where);

                    
                }

                 //查询动态
                $r = $this->get_trend($u_r['id'],1);
				//print_r($r);
                if($r){
                     //时间排序
                    $sort = array(  
                    'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序  
                    'field'     => 'add_time',       //排序字段  
                    ); 

                    $arrSort = array();  
                    foreach($r AS $uniqid => $row){  
                        foreach($row AS $key=>$value){  
                            $arrSort[$key][$uniqid] = $value;  
                        }  
                    }  
                    if($sort['direction']){  
                        array_multisort($arrSort[$sort['field']],constant($sort['direction']),$r);  
                    }
                    $r=array_slice($r,0,5);
                }
                //print_r($r);       
                //统计师父数
                $map = array();
                $map['user_id'] = $id;
                $map['status'] = 1;
                $sf_count = D('AdviserFollow')->count($map);

                $chat_count = $this->get_friends(1);
                //推荐平台
                $map = array();
                $map['status']=0;
                $platform_list= D('Platform')->get_all($map);
                foreach ($platform_list as $k => $v) {
                    $map = '';
                    $map['platform_id'] = $v['id'];
                    $platform_scheme = D('PlatformScheme')->where($map)->select();
                    //查询红包价格
                    foreach ($platform_scheme as $ke => $va) {
                       $scheme_id = $va['id'];
                       $map = array();
                       $map['user_id'] = $_SESSION['user']['id'];
                       $map['scheme_id'] = $scheme_id;
                       $redpacket = D('RedPacket')->get_one($map);

                       if($redpacket){
                            $platform_scheme[$ke]['p_user_award'] = $redpacket['user_award'];
                            $platform_scheme[$ke]['p_push_award'] = $redpacket['push_award'];
                       }else{
                            $platform_scheme[$ke]['p_user_award'] = $va['user_award'];
                            $platform_scheme[$ke]['p_push_award'] = $va['push_award'];
                       }
                    }
                    $platform_list[$k]['scheme'] = $platform_scheme;
				}
                $this->assign('packet',$packet);
                $this->assign('platform_list',$platform_list);
                $this->assign('chat_count',$chat_count);
                $this->assign('r',$r);
                $this->assign('user',$u_r);
                $this->assign('r_info',$r_info);
                $this->assign('sf_count',$sf_count);
                $this->display('Person/center_self');
            }else{
             header("location:/Home/User/login");
            }
        }else{
            //判断身份
            $map['id'] = $id;
            $u_r = D('User')->get_one($map);
            $role = $u_r['role'];
            //if($role==2){
                //查询推手ID
               

                if($role==2){
                    $map = array();
                    $map['user_id'] = $u_r['id'];
                    $r_info = D('Recommend')->get_one($map);
                    $recommend_id = $r_info['id'];
                    $p_list = $this->get_push_plat($recommend_id);
                    $packet = R('Packet/get_redpacket',array('userid'=>$u_r['id'],'platform_id'=>''));
					foreach($packet as $k=>$v){
						
					$map = array();
					$map['platform_id'] = $v['platform_info']['id'];
					$packet[$k]['circle'] = D('Circle')->get_one($map);
					//echo D('Circle')->_sql();
					
					}
					//print_r($packet);
                      //统计徒弟数量
                    $where = array();
                    $where['referral'] = $u_r['id'];
                    $r_info['fans_count']=D('User')->count($where);
                    
                }
                    //查询动态
                    $r = $this->get_trend($u_r['id'],0);
					
                    if($r){
                         //时间排序
                        $sort = array(  
                        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序  
                        'field'     => 'add_time',       //排序字段  
                        ); 

                        $arrSort = array();  
                        foreach($r AS $uniqid => $row){  
                            foreach($row AS $key=>$value){  
                                $arrSort[$key][$uniqid] = $value;  
                            }  
                        }  
                        if($sort['direction']){  
                            array_multisort($arrSort[$sort['field']],constant($sort['direction']),$r);  
                        }
                        $r=array_slice($r,0,5);
                    }
                    
                    //print_r($r);
                    //统计师父数
                    $map = array();
                    $map['user_id'] = $id;
                    $map['status'] = 1;
                    $sf_count = D('AdviserFollow')->count($map);

                    //判断师徒关系
                    if($_SESSION['user']['id']){
                        $map = array();
                        $map['user_id'] = $_SESSION['user']['id'];
                        $map['recommend_id'] = $r_info['id'];
                        $map['status'] = 1;
                        $fans = D('AdviserFollow')->get_one($map);
                        if($fans){
                            $is_fans = 1;
                        }else{
                            $is_fans = 0;
                        }
                    }else{
                        $is_fans = 0;
                    }
                  //  print_r($packet);die;
                 $this->assign('packet',$packet);
                $this->assign('is_fans',$is_fans);
                $this->assign('sf_count',$sf_count);
                $this->assign('r',$r);
                $this->assign('p_list',$p_list);
                $this->assign('user',$u_r);
                $this->assign('r_info',$r_info);
                $this->display();
            //}

        }
        
        
    }

    function get_more_trend(){
        $user_id = I('post.user_id');
        $is_self = I('post.is_self');
        //查询动态
        $r = $this->get_trend($user_id,$self);
        $p = I('post.p');
        $pagenum = 5;
        $p = $p*$pagenum;

        if($r){
             //时间排序
            $sort = array(  
            'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序  
            'field'     => 'add_time',       //排序字段  
            ); 

            $arrSort = array();  
            foreach($r AS $uniqid => $row){  
                foreach($row AS $key=>$value){  
                    $arrSort[$key][$uniqid] = $value;  
                }  
            }  
            if($sort['direction']){  
                array_multisort($arrSort[$sort['field']],constant($sort['direction']),$r);  
            }
            $r=array_slice($r,$p,$pagenum);
            $this->assign('r',$r);
            if($is_self!=1){
                $content = $this->fetch('noself_trend_item');
            }
            if($r){
                $this->ajaxReturn(array('content'=>$content));
            }
        }

    }
      public function ajax_get_trend(){
        $userid = I('post.userid');
        $is_self = I('post.is_self');
        $trend = $this->get_trend($userid,$is_self);
        $val = I('post.val');
        $p = I('post.p');
        $pagenum = 5;
		$p = ($p)*$pagenum;
        $pagenum = $p+$pagenum-1; 
		if($val){
			$pagenum = 5;
			$p = I('post.p');
			$p = $p-1;
			$p = $p*$pagenum;
			$pagenum = $p+$pagenum-1; 
		}
        if($val==2){
            //点赞
            $type_arr =array('14','15','16','17');
        }elseif($val==1){
            //回复
            $type_arr =array('10','11','12','13');
            
        }elseif($val==3){
            //关注
             $type_arr =array('8','9');
        }
		//print_r($)
        if($val){
            foreach ($trend as $k => $v) {
               if($val==4){
                if($v['user_id']!=$_SESSION['user']['id']){
                    $arr[] = $v;
                }
               }elseif($val==5){
                $arr[] = $v;
               }else{
                if(in_array($v['type_id'],$type_arr)){
                    $arr[] = $v;
                }
               }
                


            }
            //echo $p."**".$pagenum;
            $arr=array_slice($arr,$p,$pagenum);
			//print_r($p);die;
            $this->assign('r',$arr);
            if($arr){
				//print_r($arr);die;
                $content = $this->fetch('my_active_list');
            }else{
                $content =0;
            }
            
            $this->ajaxReturn(array('content'=>$content));
        }else{
           // $trend=array_slice($trend,$p,$pagenum);
            //print_r($trend);die;
            $this->assign('list',$trend);
            $content = $this->fetch('trend_list');
            $this->ajaxReturn(array('content'=>$content));
        }
        

       
    }
    /**
     * [get_push_plat 获取圈子列表]
     * @param  [type] $recommend_id [投顾ID]
     * @return [type]               [description]
     */
    public function get_push_plat($recommend_id){
        //获取平台
        //$recommend_id = 40;
        $map['recommend_id'] = $recommend_id;
        $list = D('RecommendPlatform')->get_all($map);
        //查询圈子信息
        foreach ($list as $k => $v) {
            $platform_id = $v['platform_id'];
            $map['platform_id'] = $platform_id;
            $p_list_info= D('Circle')->get_one($map);
            if($p_list_info){
                $arr[$platform_id] =$p_list_info;
            }

            foreach ($arr as $k => $v) {
                $arr[$k]['leve'] = R('Circle/get_cricel_leve',array('platform_id'=>$v['platform_id']));
            }
        }
        $arr = array_values($arr);
        return $arr;
    }

    /**
     * [get_trend description]
     * @param  [type] $userid  [description]
     * @param  [type] $is_self [description]
     * @return [type]          [description]
     */
    public function get_trend($userid,$is_self){
        //查询关注的用户
        $map['user_id'] = $userid;
        $recommend = D('Recommend')->get_one($map);
        $map = array();
        if($recommend){
            $map['_string'] = 'recommend_id='.$recommend['id'].' OR user_id='.$userid;
            $map['status'] = 1;
        }else{
            $map['user_id'] = $userid;
            $map['status'] = 1;
        }
        // print_r($map);die;
        $r_list = D('AdviserFollow')->where($map)->select();

        //echo D('AdviserFollow')->_sql();
        if($r_list){
            // $user_arr[]=$r_list['user_id'];
            foreach ($r_list as $k => $v) {
                $map = array();
                $map['id'] = $v['recommend_id'];
                $u = D('Recommend')->get_one($map);
                $user_arr[] = $u['user_id'];
            }
        }
        $user_arr[] = $userid;
      // print_r($user_arr);
        //短评
        if($is_self){
            $map = array();
            $map['user_id'] = array('in',$user_arr);
        }else{
            $map = array();
            $map['user_id'] = $userid;
        }
       //print_r($user_arr);die;
        $stor_comment = D('PlatformCommentary')->get_all($map);

        foreach ($stor_comment as $k => $v) {
           $stor_comment[$k]['url'] = "/Home/Circle/commentary_info/id/".$v['id'];
           $stor_comment[$k]['tage'] = "短评";
           $stor_comment[$k]['type'] = 1;
           $where['id'] = $v['platform_id'];
           $plat_info = D('Platform')->get_one($where);
           $stor_comment[$k]['platname'] = $plat_info['name'];
           $maps['id'] = $v['user_id'];
           $stor_comment[$k]['user'] = D('User')->get_one($maps);
           $stor_comment[$k]['tag'] = explode(',',$v['tag']);

            if($_SESSION['user']['id']){
                    $stor_comment[$k]['is_praise'] = $this->praise_is($_SESSION['user']['id'],6,$v['id']);
            }else{
                    $stor_comment[$k]['is_praise'] = 0;
            }

        }
        //信批
        $announce = D('CircleAnnounce')->get_all($map);
        foreach ($announce as $k => $v) {
           $announce[$k]['url'] = "/Home/Circle/infoann_info/id/".$v['id'];
           $announce[$k]['type'] = 2;
           $announce[$k]['tage'] = "信批解读";
           $where['id'] = $v['platform_id'];
           $plat_info = D('Platform')->get_one($where);
           $announce[$k]['platname'] = $plat_info['name'];
           $maps['id'] = $v['user_id'];
           $announce[$k]['user'] = D('User')->get_one($maps);

           if($_SESSION['user']['id']){
                    $announce[$k]['is_praise'] = $this->praise_is($_SESSION['user']['id'],9,$v['id']);
            }else{
                    $announce[$k]['is_praise'] = 0;
            }
        }

        //舆情
        $opinion = D('CircleOpinion')->get_all($map);
        foreach ($opinion as $k => $v) {
           $opinion[$k]['url'] = "/Home/Circle/circle_opinion_info/id/".$v['id'];
           $opinion[$k]['type'] = 3;
           $opinion[$k]['tage'] = "舆情解读";
           $where['id'] = $v['platform_id'];
           $plat_info = D('Platform')->get_one($where);
           $opinion[$k]['platname'] = $plat_info['name'];
           $maps['id'] = $v['user_id'];
           $opinion[$k]['user'] = D('User')->get_one($maps);
           if($_SESSION['user']['id']){
                    $opinion[$k]['is_praise'] = $this->praise_is($_SESSION['user']['id'],8,$v['id']);
            }else{
                    $opinion[$k]['is_praise'] = 0;
            }
        }

        //深度
        $depth = D('Depth')->get_all($map);
        foreach ($depth as $k => $v) {
           $depth[$k]['url'] = "/Home/Circle/depth_info/id/".$v['id'];
           $depth[$k]['type'] = 4;
           $depth[$k]['tage'] = "深度评测";
           $where['id'] = $v['platform_id'];
           $plat_info = D('Platform')->get_one($where);
           $depth[$k]['platname'] = $plat_info['name'];
           $maps['id'] = $v['user_id'];
           $depth[$k]['user'] = D('User')->get_one($maps);
           if($_SESSION['user']['id']){
                    $depth[$k]['is_praise'] = $this->praise_is($_SESSION['user']['id'],7,$v['id']);
            }else{
                    $depth[$k]['is_praise'] = 0;
            }
        }
         $arr = array_merge($stor_comment,$announce,$opinion,$depth);
         //print_r($arr);
         foreach ($arr as $k => $v) {
             $user = $v['user_id'];
             $map_s['user_id'] = $user;
             $map_s['platform_id'] = $v['platform_id'];
             $arr[$k]['grade'] = D('PlatformGrade')->get_one($map_s);
         }
        if($is_self==1){
            //如果是自己进入主页还要合并站内消息
            $map['user_id'] = $userid;
            $map['type_id'] = array('in','7,8,9,10,11,12,13,14,15,16,17');
            $msg = D('Message')->get_all($map);
            foreach ($msg as $k => $v) {
                    $where['id'] =$v['t_user_id'];
                    $msg[$k]['user']  = D('User')->get_one($where);
                if($v['type_id'] == 10 or $v['type_id'] == 17){
                    //h回复 点赞为 短评
                    $maps['id'] = $v['t_id'];
                    $con = D('PlatformCommentary')->get_one($maps);
                    if($_SESSION['user']['id']){
                        $con['is_praise'] = $this->praise_is($_SESSION['user']['id'],3,$v['id']);
                    }else{
                        $con['is_praise'] = 0;
                    }

                    $msg[$k]['info'] = $con;
                    $msg[$k]['info']['tage'] = "短评";
                    $wheres['id'] = $con['platform_id'];
                    $plat_info = D('Platform')->get_one($wheres);
                    $msg[$k]['info']['platname'] = $plat_info['name'];

                    $msg[$k]['url'] = "/Home/Circle/commentary_info/id/".$con['id'];
                    $msg[$k]['type'] = 5;
                }elseif($v['type_id'] == 11 or $v['type_id'] == 14){
                    //h回复 点赞为 深度
                    
                    $maps['id'] = $v['t_id'];
                    $con = D('Depth')->get_one($maps);
                    $msg[$k]['info'] = $con;
                    $msg[$k]['info']['tage'] = "深度测评";
                    $wheres['id'] = $con['platform_id'];
                    $plat_info = D('Platform')->get_one($wheres);
                    $msg[$k]['info']['platname'] = $plat_info['name'];

                    $msg[$k]['url'] = "/Home/Circle/depth_info/id/".$con['id'];
                    $msg[$k]['type'] = 6;
                }elseif($v['type_id'] == 12 or $v['type_id'] == 15){
                    //h回复 点赞为 舆情
                    $maps['id'] = $v['t_id'];
                    $con = D('CircleOpinion')->get_one($maps);
                    $msg[$k]['info'] = $con;
                    $msg[$k]['info']['tage'] = "舆情解读";
                    $wheres['id'] = $con['platform_id'];
                    $plat_info = D('Platform')->get_one($wheres);
                    $msg[$k]['info']['platname'] = $plat_info['name'];

                    $msg[$k]['url'] = "/Home/Circle/circle_opinion_info/id/".$con['id'];
                    $msg[$k]['type'] = 7;
                }elseif($v['type_id'] == 13 or $v['type_id'] == 16){
                    //h回复 点赞为 信批
                    $maps['id'] = $v['t_id'];
                    $con = D('CircleAnnounce')->get_one($maps);
                    $msg[$k]['info'] = $con;
                    
                    $msg[$k]['info']['tage'] = "信批解读";
                    $wheres['id'] = $con['platform_id'];
                    $plat_info = D('Platform')->get_one($wheres);
                    $msg[$k]['info']['platname'] = $plat_info['name'];
                    $msg[$k]['url'] = "/Home/Circle/infoann_info/id/".$con['id'];
                    $msg[$k]['type'] = 8;
                }elseif($v['type_id'] == 8){
                    //拜师
                    $msg[$k]['type'] = 9;
                }elseif($v['type_id'] == 9){
                    //拜师
                    $msg[$k]['type'] = 10;
                }

                if($v['type_id'] == 10 or $v['type_id'] == 11 or $v['type_id'] == 12 or $v['type_id'] == 13){
                    $commentid = $v['comment_id'];
                    $map = array();
                    $map['id'] = $commentid;
                    $comment_info = D('Comment')->get_one($map);
                    $msg[$k]['comment_count'] = $comment_info['content'];
                    $msg[$k]['comment_praise'] = $comment_info['praise_count'];

                    if($_SESSION['user']['id']){
                        $msg[$k]['is_praise'] = $this->praise_is($_SESSION['user']['id'],3,$v['comment_id']);
                    }else{
                        $msg[$k]['is_praise'] = 0;
                    }

                }
            }
            //print_r($msg);
            

             $arr = array_merge($arr,$msg);
        }

        foreach ($arr as $k => $v) {
            $add_date = $v['add_time'];
            $month = ceil(date('m',$add_date));
            $month_str='一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月';  
            $arr_m=explode(',',$month_str);
            foreach($arr_m as $key=>$val){  
            if($key+1==$month){  
                $arr[$k]['zh_data_m']= $val;  
                $arr[$k]['m']= date('y-m-d',$add_date);  
                }  
            }  
      
        }
       
        return $arr;
    }
    /**
     * 判断点赞
     */
    
    public function praise_is($userid,$type,$id){
        $where['id_user'] = $userid;
        $where['type'] = $type;
        $where['id_target'] = $id;
        $praise = D('Praise')->get_one($where);
        if($praise){
            $is_praise = 1;
        }else{
            $is_praise = 0;
        }
        return $is_praise;

    }

    /**
     * [get_friends 获取好友列表]
     * @return [type] [description]
     */
    public function get_friends($val=0){
        $list = R('Im/firend_list');
       // return $list;die;
        //获取最后一天聊天记录
        $chat_count = 0;
        foreach ($list as $k => $v) {
            if($v['type']==1){
                $map = array();
                $map['_string'] = "send_id =".$v['user_info']['id']." or receive_id = ".$v['user_info']['id'];
            }else{
                $map = array();
                $map['group_id'] = $v['group_info']['id'];
				if($v['group_info']['platform_id']==0){
                    $list[$k]['type']=3;
                    $list[$k]['group_main']=str_replace("88", "",$v['group_id']);
                }
            }
            $chat = D('Chat')->where($map)->order('add_time desc')->find();
            if($chat){
                $list[$k]['lats_chat'] = $chat;
                $list[$k]['lats_chat']['content'] = msubstr($chat['content'], 0, 20, $charset="utf-8", $suffix=true) ;
            }else{
                $list[$k]['lats_chat']['content']='暂无新消息';
            }
           $chat_count = $chat_count+$v['is_noread_count'];
		  
        }
	  foreach ($list as $k => $v) {
             if($v['group_id'] && !$v['group_info']) unset($list[$k]);
    		 if($v['type']==1 && !$v['user_info']) unset($list[$k]);
    	 } 
        $sort = array(  
        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序  
        'field'     => 'is_noread_count',       //排序字段  
        ); 

         $arrSort = array();  
        foreach($list AS $uniqid => $row){  
            foreach($row AS $key=>$value){  
                $arrSort[$key][$uniqid] = $value;  
            }  
        }  
        if($sort['direction']){  
            array_multisort($arrSort[$sort['field']],constant($sort['direction']),$list);  
        }  
      
        if($val==1){
            return $chat_count;
        }else if($val==3){
			//echo 1;die;
			return $list;
		}else{
			$this->ajaxReturn($list);
        }
    }
    
    
    //推手编辑网贷信息
    public function edit_profile() {
        if(IS_POST) {
            $user_id = $_SESSION['user']['id'];
        //    print_r($user_id);die;
            if($user_id) {
                $map['user_id'] = $user_id;
                $recom = D('Recommend')->get_one($map);
                if($recom) {
                    $postdata = I('post.');
                    $map = array();
                    $map['id'] = $recom['id'];
                    $data = D('Recommend')->edit($map,$postdata);
                    if($data) {
                        $res = array('err'=>0,'msg'=>"提交成功");
                    } else {
                        $res = array('err'=>1,'msg'=>"提交失败");
                    }
                } else {
                    $res = array('err'=>3,'msg'=>"您不是投顾");
                }
            } else {
                $res = array('err'=>2,'msg'=>"请先登录");
            }
            $this->ajaxReturn($res);
        } else {
            $id = $_GET['id'];
            $map['user_id'] = $id;
            $recom = D('Recommend')->get_one($map);
            $this->assign('recom',$recom);
            $this->display();
        }
    }

}